package people.paper.tv.activity.home.modual.ydtj.view.impl;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import java.lang.ref.WeakReference;
import java.util.List;

import people.paper.tv.BaseApplication;
import people.paper.tv.activity.home.HomeActivity;
import people.paper.tv.activity.home.HomeFragment;
import people.paper.tv.activity.home.modual.ydtj.presenter.YDTJPresenter;
import people.paper.tv.activity.home.modual.ydtj.view.YDTJView;
import people.paper.tv.activity.home.modual.ywyl.view.data.NewsData;
import people.paper.tv.activity.home.modual.ywyl.view.impl.FocusNewsViewLayout;
import people.paper.tv.activity.home.modual.ywyl.view.impl.IViewCallBack;

public class YDTJViewImpl implements YDTJView, IViewCallBack {
    Context context;
    YDTJPresenter presenter;
    public HomeFragment homeFragment;
    FocusNewsViewLayout layout;
    YDTJViewImpl.UiDataHandler handler;
    static final int MSG_LOAD_MORE_DATA = 0;
    boolean isDateLoaded = false;

    protected static class UiDataHandler extends Handler {
        private static WeakReference<YDTJViewImpl> ref;
        public UiDataHandler(YDTJViewImpl impl) {
            ref = new WeakReference<YDTJViewImpl>(impl);
        }

        @Override
        public void handleMessage(Message msg) {
            if (ref.get() == null) {
                return;
            }
            switch (msg.what) {
                case MSG_LOAD_MORE_DATA:
                    Log.i("YDTJViewImpl onNewsLoad", "YDTJViewImpl UiDataHandler:"+msg.arg1);
                    ref.get().presenter.showPageAt(msg.arg1);
                    break;
            }
        }
    }


    @Override
    public View create(Context context, YDTJPresenter presenter, HomeFragment homeFragment) {
        this.context = context;
        this.presenter = presenter;
        this.homeFragment = homeFragment;
        handler = new YDTJViewImpl.UiDataHandler(this);
        layout = new FocusNewsViewLayout(context);
        layout.setClipChildren(false);
        layout.setClipToPadding(false);
        layout.setIViewCallBack(this);
        return layout;
    }

    public boolean setLayoutFocus(){
        return layout.setLayoutFocus();
    }

    @Override
    public boolean changeFragmentFocus(boolean isLeft) {
        Log.i("fragment", "ydtj changeFragmentFocus:"+isDateLoaded);
        if(isDateLoaded && layout != null){
            return layout.changeFocus(isLeft);
        }else {
            return false;
        }
    }


    @Override
    public void resume() {
        if(layout != null)
            layout.resume();
    }

    @Override
    public void pause() {
        if(layout != null)
            layout.pause();
    }

    @Override
    public void onActivityResume() {
        if(layout != null)
            layout.resume();
    }

    @Override
    public void onActivityPause() {
        if(layout != null)
            layout.pause();
    }

    @Override
    public void destroy() {
        if(layout != null)
            layout.destroy();
    }

    @Override
    public void preFragment() {
        homeFragment.prev(HomeActivity.YDTJ_INDEX);
    }

    @Override
    public void nextFragment() {
        homeFragment.next(HomeActivity.YDTJ_INDEX);
    }

    @Override
    public void getNextPageData(int page) {
        getNextPage(page);
    }

    @Override
    public void changeFocus() {
        homeFragment.homeView.changeFocusToBar(HomeActivity.YDTJ_INDEX);
    }

    @Override
    public View getParentView() {
        return layout;
    }

    @Override
    public YDTJViewImpl getView() {
        return this;
    }

    @Override
    public void showError(int errCode) {
        BaseApplication.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                layout.showError();
                layout.hideLoading();
            }
        });
    }

    @Override
    public void onNewsLoaded(final int pageIndex, final List<NewsData> datas) {
        Log.i("YDTJViewImpl onNewsLoad", "YDTJViewImpl onNewsLoad:"+pageIndex+"; "+datas);
        BaseApplication.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(datas != null && datas.size() > 0){
                    isDateLoaded = true;
                    layout.refresh(pageIndex, datas);
                }else {
                    layout.showError();
                    layout.hideLoading();
                }
            }
        });
    }

    public void getNextPage(int page) {
        Log.i("YDTJViewImpl onNewsLoad", "YDTJViewImpl getNextPage UiDataHandler:"+page);
        if (handler.hasMessages(MSG_LOAD_MORE_DATA))
            handler.removeMessages(MSG_LOAD_MORE_DATA);
        Message msg = handler.obtainMessage(MSG_LOAD_MORE_DATA);
        msg.what = MSG_LOAD_MORE_DATA;
        msg.arg1 = page;
        handler.sendMessageDelayed(msg, 200);
    }
}
